import java.util .Scanner;
public class test_3_21 {
    public static void main1(String[] args) {
        String s1 = "Welcome to Java";
        String s2 = "Programming is fun";
        String s3 = "Welcome to Java";
        String s4 = s1 +s2;
        System.out.println(s1.compareTo(s2));
        System.out.println(s2.compareTo(s2));
        System.out.println(s1.charAt(0));
        System.out.println(s1.indexOf("to"));
        System.out.println(s1.indexOf('j'));
        System.out.println(s1.toLowerCase());
        System.out.println(s1.toUpperCase());
        System.out.println(s1.concat(s2));
        System.out.println(s1.contains(s2));
        System.out.println("\t Wel \t".trim());
        System.out.println(s4);
        }

    public static void main2(String[] args) {
        String s1 = "Welcome to Java";
        String s2 = "Programming is fun";
        String s3 = "Welcome to Java";
        char a = s1.charAt(s1.length() - 1);
/*
        System.out.println(a);
        System.out.println("1" + 1);
        System.out.println('1' + 1);
        System.out.println("1" + 2 + 1);
        System.out.println("1" + (1 + 1));
        System.out.println('1' + 1 + 1);
*/
        System.out.println(1 + "Welcome " + ('\u0001' + 1));
    }

    public static void main3(String[] args) {
        String s1 = "Welcome";
        String s2 = "welcome";
        boolean isEqual = s1.equals(s2);
        boolean isEqual2 = s1.equalsIgnoreCase(s2);
        System.out.println(s1.substring(1,2));
        System.out.println(s1.charAt(1));
    }

    public static void main4(String[] args) {
        double i = 10.2;
        String a = 10.2 + "";
        int x = a.length() - 1;
        System.out.println(x);
    }

    public static void main5(String[] args) {
        String set1 ="1 3 5 7\n"+"9 11 13 15\n"+"17 19 21 23\n"+"25 27 29 31";
        String set2="2 3 6 7\n"+"10 11 14 15\n"+"18 19 22 23\n"+"26 27 30 31";
        String set3 ="4 5 6 7\n"+"12 13 14 15\n"+"20 21 22 23\n"+"28 29 30 31";
        String set4="8 9 10 11\n" + "12 13 14 15\n"+"24 25 26 27\n"+"28293031";
        String set5="16 17 18 19\n"+ "20 21 22 23\n"+"24 25 26 27\n"+"28 29 30 31";
        int day = 0;
        // Create a Scanner
        Scanner input = new Scanner(System.in);
        // Prompt the user to answer questions
        System.out.print("Is your birthday in Set1?in");
        System.out.print(set1);
        System.out.print("InEnter 0 for No and 1 for Yes:");
        int answer=input.nextInt();
        if (answer==1)
            day+=1;
        // Prompt the user to answer questions
        System.out.print("InIs your birthday in Set2?\n");
        System.out.print(set2);
        System.out.print("InEnter 0 for No and 1 for Yes:");
        answer = input.nextInt();
        if (answer==1)
            day+=2;
        // Prompt the user to answer questions
        System.out.print("InIs your birthday in Set3?\n");
        System.out.print(set3);
        System.out.print("\nEnter 0 for No and 1 for Yes:");
        answer = input.nextInt();
        if (answer == 1)
        day+=4;
// Prompt the user to answer questions
        System.out.print("nIs your birthday in Set4?\n");
        System.out.print(set4);
        System.out.print("InEnter 0 for No and 1 for Yes:");
        answer=input.nextInt();
        if(answer==1)
            day+=8;
// Prompt the user to answer questions
        System.out.print("nIs your birthday in Set5?\n");
        System.out.print(set5);
        System.out.print("InEnter 0 for No and 1 for Yes:");
        answer =input.nextInt();
        if(answer==1)
            day += 16;
        System.out.println("\nYour birthday is"+ day +"!");
    }

    public static void main6(String[] args) {
        System.out .printf("amount is %f %e\n",32.32, 32.32);
        System.out.printf("amount is %5.2f%% %5.4e\n",32.327,32.32);
        System.out.printf("%6b\n",(1 > 2));
        System.out.printf("%6s\n","Java");
        System.out.printf("%-6b%s\n",(1 > 2),"Java");
        System.out.printf("%6b%-8s\n",(1 > 2),"Java");
        System.out.printf("%,5d %,6.1f\n",312342,315562.932);
        System.out.printf("%05d %06.1f\n", 32, 32.32);
    }

    public static void main7(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an ASCII code:");
        int a = input.nextInt();
        System.out.printf("The character for ASCII code %d is %c", a, a);
    }



//    public static void main(String[] args)
//    {
//        Scanner input = new Scanner(System.in);
//        System.out.print("Enter a hex digit:");
//        String hexString = input.nextLine();
//        if (hexString.length() != 1) {
//            System.out.println("You must enter exactly one character");
//            System.exit(1);
//        }
//
//         char ch = Character.toUpperCase(hexString.charAt(0));
//        if('A'<= ch && ch <='F') {
//            int value = ch - 'A' + 10;
//            System.out.println("The decimal value for hex digit " + ch + "is " + value);
//        }
//        else if (Character.isDigit(ch)) {
//            System.out.println("The decimal value for hex digit " + ch + "is " + ch);
//        }
//        else{
//            System.out.println(ch +" is an invalid input");
//    }

//    }
        public static void main(String[] args) {
            try(Scanner input = new Scanner(System.in);) {
                System.out.print("Enter a hex digit: ");
                String hex = input.next();

                System.out.println((isHex(hex)) ? "The binary value is "+binaryValue(hex) :
                        hex+" is an invalid input");
            }
        }

        //判定是否为十六进制数
        public static boolean isHex(String hex) {
            int digit;
            try {	//字符串直接转换为数字，存在字符时会自动抛出异常
                digit = Integer.parseInt(hex);
            }catch (Exception e) {	//字符串存在字母时，捕获异常并转换为数字
                digit = hex.charAt(0) - 'A' + 10;
            }

            return (0 <= digit && digit <= 15) ? true : false;
        }

        //获取二进制
        public static String binaryValue(String hex) {
//		String binary = "";
//		switch(hex) {
//		case "0": binary = "0000"; break;
//		case "1": binary = "0001"; break;
//		case "2": binary = "0010"; break;
//		case "3": binary = "0011"; break;
//		case "4": binary = "0100"; break;
//		case "5": binary = "0101"; break;
//		case "6": binary = "0110"; break;
//		case "7": binary = "0111"; break;
//		case "8": binary = "1000"; break;
//		case "9": binary = "1001"; break;
//		case "A": binary = "1010"; break;
//		case "B": binary = "1011"; break;
//		case "C": binary = "1100"; break;
//		case "D": binary = "1101"; break;
//		case "E": binary = "1110"; break;
//		case "F": binary = "1111"; break;
//		}
//		return binary;

            //创建字符串缓冲器对象
            StringBuffer stringBuffer = new StringBuffer(6);
            int digit;
            try {
                digit = Integer.parseInt(hex);
            }catch (Exception e) {
                digit = hex.charAt(0) - 'A' + 10;
            }

            while(digit != 0) {	//转换为二进制
                stringBuffer.append(digit % 2);
                digit /= 2;
            }

            //二进制长度小于4时补0
            while(stringBuffer.length() < 4) stringBuffer.append(0);
            return stringBuffer.reverse().toString();	//返回逆转的字符串
        }
    }


